package com.xizi.collection;

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArraySet;

class PutThread extends Thread {
    private CopyOnWriteArraySet<Integer> cowas;

    public PutThread(CopyOnWriteArraySet<Integer> cowas) {
        this.cowas = cowas;
    }

    public void run() {
        for (int i = 0; i < 10; i++) {
            cowas.add(i);
        }
    }
}

public class CopyOnWriteArraySetDemo {
    public static void main(String[] args) {
        CopyOnWriteArraySet<Integer> cowas = new CopyOnWriteArraySet<>();
        for (int i = 0; i < 10; i++) {
            cowas.add(i);
        }
        PutThread p1 = new PutThread(cowas);
        p1.start();
        Iterator<Integer> iterator = cowas.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        iterator = cowas.iterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
    }
}